package com.jiefutong.zyt;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.jiefutong.zyt.bean.LoginBean;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.zhy.autolayout.AutoLayoutActivity;

public class BackSetPasswordActivity extends AutoLayoutActivity {
    private EditText passwordET;
    private EditText passwordAgainET;

    private CheckBox hidepassword;

    private CheckBox hidepasswordAgain;
    private Button submitBtn;
    private String userMobile;
    private String code;
    private ImageView backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_back_set_password);
        userMobile=getIntent().getStringExtra("userMobile");
        code=getIntent().getStringExtra("code");
        initView();

        hidepassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = passwordET.getText().length();
                if(isChecked){
                    passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else{
                    passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                passwordET.setSelection(passwordLength);
            }
        });

        hidepasswordAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = passwordAgainET.getText().length();
                if(isChecked){
                    passwordAgainET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else{
                    passwordAgainET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                passwordAgainET.setSelection(passwordLength);
            }
        });
        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadDatas();
            }
        });
    }

    private void initView() {
        backBtn=findViewById(R.id.back_btn);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        passwordET=findViewById(R.id.backSetPassword_passwordET);
        passwordAgainET=findViewById(R.id.backSetPassword_passwordAgainET);
        hidepassword=findViewById(R.id.backSetPassword_hidepassword);
        hidepasswordAgain=findViewById(R.id.backSetPassword_hidepasswordAgain);
        submitBtn=findViewById(R.id.backSetPassword_submitBtn);


    }

    public void loadDatas(){

        if (TextUtils.isEmpty(passwordET.getText().toString())) {
            Toast.makeText(getBaseContext(), "请输入密码", Toast.LENGTH_LONG).show();
            return;
        }
        if (TextUtils.isEmpty(passwordAgainET.getText().toString())) {
            Toast.makeText(getBaseContext(), "请再次输入密码", Toast.LENGTH_LONG).show();
            return;
        }


        if(!passwordET.getText().toString().equals(passwordAgainET.getText().toString())){
            Toast.makeText(getBaseContext(), "两次密码输入不一致", Toast.LENGTH_LONG).show();
            return;
        }
        showProgress(true);

        HttpParams params = new HttpParams();
        params.put("userMobile", userMobile);
        params.put("password", passwordET.getText().toString());
        params.put("code", code);
        OkGo.<String>post("https://zyt.jiefutong.net/api/index/api/user/change/passsword")
                .tag(this)
                .params(params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        final LoginBean bean = JSON.parseObject(response.body(), LoginBean.class);
                        if (bean != null && bean.status.equals("success")) {
                            Toast.makeText(getBaseContext(),"设置成功", Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(BackSetPasswordActivity.this,LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }else{
                            Toast.makeText(getBaseContext(),bean.message, Toast.LENGTH_SHORT).show();
                        }
                        showProgress(false);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showProgress(false);
                    }
                });
    }


    /**
     * Shows the progress UI and hides the login form.
     */
    private ProgressDialog progress;
    public void showProgress(final boolean show) {
        if (progress == null) {
            progress = new ProgressDialog(
                    BackSetPasswordActivity.this);
            progress.setMessage("正在加载...");
            progress.setCanceledOnTouchOutside(false);
        }

        if (show) {
            progress.show();
        } else {
            progress.dismiss();
        }
    }
}
